package ArrayStack;

import java.util.Arrays;

public class MyStack implements IStack {
    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;
    public MyStack(){
        this.elem=new int[DEFAULT_CAPACITY];
    }
    public void Expansion(){
        if(full()){
            elem= Arrays.copyOf(elem,elem.length*2);
        }
    }

    @Override
    public void push(int x) {
        Expansion();
        elem[usedSize]=x;
        this.usedSize++;
    }

    @Override
    public int pop() {
        if(empty()){
           throw new StackEmpty("栈为空");
        }
         int ret=elem[usedSize-1];
         this.usedSize--;

         return ret;
    }

    @Override
    public int peek() {
        if(empty()){
            throw new StackEmpty("栈为空");
        }
        int ret=elem[usedSize-1];

        return ret;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public boolean empty() {
        return this.usedSize==0;
    }

    @Override
    public boolean full() {
        return this.usedSize==elem.length;
    }
}
